/**
 * Created by dqw on 2015/12/30.
 */

//列表开始
var dtGridColumns = [
    {
        id: 'delFlag',
        type: 'int',
        hideQuery:true,
        hideQueryType:'eq',
        hideQueryValue:1,
        hide:true

    },
    {
        id: 'className',
        title: '分类',
        type: 'string',
        headerClass: 'text-center width-100',
        columnClass: 'text-center width-100',
        fastSort: true,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'sortNo',
        title: '排序',
        type: 'int',
        headerClass: 'text-center width-100',
        columnClass: 'text-center width-100',
        fastSort: true
    },
    {
        id: 'status',
        title: '状态',
        type: 'int',
        headerClass: 'text-center width-100',
        columnClass: 'text-center width-100',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk',
        codeTable: {
            1: $lang.showStatus.show,
            2: $lang.showStatus.hide
        }
    },
    {
        id: 'operation',
        title: '操作',
        type: 'string',
        headerClass: 'text-center width-100',
        columnClass: 'text-center width-100',
        fastSort: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var contentHtml = "<a data-target='#contentModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-edit'></i>&nbsp;内容管理&nbsp;</a>";
            var editHtml = "<a data-target='#editClassModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-edit'></i>&nbsp;编辑&nbsp;</a>";
            var delHtml = "<a href='javascript:;' class='btn btn-danger btn-sm' onclick='OperateHandle.delHelpClass(" + record.id + ")' ><i class='fa fa fa-lg fa-trash-o'></i>&nbsp;删除&nbsp;</a>";
            var html = "";
            html += contentHtml;
            // 判断是否有编辑权限
            if ($("#helpClassEdit") && $("#helpClassEdit").val() == 1) {
                html += editHtml;
            }
            // 判断是否有删除权限
            if ($("#helpClassDel") && $("#helpClassDel").val() == 1) {
                if (record.status !== 1) {
                    html += delHtml;
                }
            }
            return html;
        }
    }
];
var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + 'helpclass/json/list',
    exportFileName: '帮助中心',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {int: ["status", "sortNo"]}
};

var grid2 = $.fn.DtGrid.init(dtGridOption);
//默认查询条件
grid2.fastQueryParameters = new Object();
//排序
grid2.sortParameter.columnId = ['desc_sortNo'];
grid2.fastQueryParameters['eq_delFlag'] = 1;
//列表结束

//操作处理结束
var dataId = '';
var grid1;
var dtGridColumnsContent = [
    {
        id: 'title',
        title: '标题',
        type: 'string',
        headerClass: 'text-center width-100',
        columnClass: 'text-center width-100',
        fastSort: true,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'content',
        title: '内容',
        type: 'string',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'sortNo',
        title: '排序',
        type: 'int',
        headerClass: 'text-left',
        columnClass: 'text-left',
        fastSort: false
    },
    {
        id: 'operation',
        title: '操作',
        type: 'string',
        headerClass: 'text-center width-200',
        columnClass: 'text-center width-200',
        fastSort: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var editHtml = "<a data-target='#editContentModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-edit'></i>&nbsp;编辑&nbsp;</a>";
            var delHtml = "<a href='javascript:;' class='btn btn-danger btn-sm refresh' onclick='OperateHandle.delHelpContent(" + record.id + ")' ><i class='fa fa fa-lg fa-trash-o'></i>&nbsp;删除&nbsp;</a>";
            var html = "";
            html += editHtml;
            html += delHtml;
            return html;

        }
    }];
$("#contentModal").on("show.bs.modal", function (event) {
    $('#dtGridContainerContent').html('');
    //获取接受事件的元素
    var button = $(event.relatedTarget);
    //获取data 参数
    var datano = button.data('no');
    var gridData = grid2.sortOriginalDatas[datano];
    dataId = gridData.id;
    var dtGridOptionContent = {
        lang: 'zh-cn',
        ajaxLoad: true,
        loadURL: ncGlobal.adminRoot + 'helpcontent/json/list/' + dataId + "/" + new Date().getTime(),
        exportFileName: '内容管理',
        columns: dtGridColumnsContent,
        gridContainer: 'dtGridContainerContent',
        toolbarContainer: 'dtGridToolBarContainerContent',
        pageSize: 10,
        pageSizeLimit: [10, 20, 50],
        ncColumnsType: {int: ['sortNo']}
    };
    grid1 = $.fn.DtGrid.init(dtGridOptionContent);
    //默认查询条件
    grid1.fastQueryParameters = new Object();
    //排序
    grid1.sortParameter.columnId = 'sortNo';
    grid1.sortParameter.sortType = 2;
    grid1.fastQueryParameters['eq_delFlag'] = 1;
    grid1.load();
    $("#currentClassId").val(gridData.id);
});


//操作处理开始
var OperateHandle = function () {
    var _ueAdd, _ueEdit;

    function _bindEvent() {

        //新增分类对话框初始化
        $("#addModal").on("show.bs.modal", function (event) {
            $("#addClassForm").on("nc.formSubmit.success", function (e) {
                grid2.refresh(true);
            });
            //清除错误信息
            $(".alert-danger").remove();
            $("#addClassForm").psly().reset();
            $("#addClassForm").find("[name='className']").val("");
        });
        //编辑分类对话框初始化
        $("#editClassModal").on("show.bs.modal", function (event) {
            $("#editClassForm").on("nc.formSubmit.success", function (e) {
                grid2.refresh(true);
            });
            //清除错误信息
            $(".alert-danger").remove();
            $("#editClassForm").psly().reset();
            //获取接受事件的元素
            var button = $(event.relatedTarget);
            //获取data 参数
            var datano = button.data('no');
            //获取列表框中的原始数据
            var gridData = grid2.sortOriginalDatas[datano];
            $editClassForm = $("#editClassForm");
            $editClassForm.find("[name='id']").val(gridData.id);
            $editClassForm.find("[name='sortNo']").val(gridData.sortNo);
            $editClassForm.find("[name='className']").val(gridData.className);
            if (gridData.status == 1) {
                $('#specialState3').attr('checked', true);
            } else {
                $('#specialState4').attr('checked', true);
            }
        });
        //内容编辑对话框初始化
        $("#editContentModal").on("show.bs.modal", function (event) {
            //清除错误信息
            $(".alert-danger").remove();
            $("#editContentForm").psly().reset();
            //获取接受事件的元素
            var button = $(event.relatedTarget);
            //获取data 参数
            var datano = button.data('no');
            //获取列表框中的原始数据
            var gridData = grid1.sortOriginalDatas[datano];
            $editContentForm = $("#editContentForm");
            $editContentForm.find("[name='id']").val(gridData.id);
            $editContentForm.find("[name='classId']").val(gridData.classId);
            $editContentForm.find("[name='sortNo']").val(gridData.sortNo);
            $editContentForm.find("[name='title']").val(gridData.title);
            if (gridData.content !== null) {
                OperateHandle.ueEditContent.setContent(gridData.content);
            }
        });
        $("#editContentForm").on("nc.formSubmit.success", function (e) {
            grid1.refresh(true);
        });
        //新增内容对话框初始化
        $("#addContentModal").on("show.bs.modal", function (event) {
            $("#addContentForm").on("nc.formSubmit.success", function (e) {
                grid1.refresh(true);
            });
            //清除错误信息
            $(".alert-danger").remove();
            $("#addContentForm").psly().reset();
            $addContentForm = $("#addContentForm");
            $addContentForm.find("[name='title']").val("");
            $addContentForm.find("[name='sortNo']").val("");
            OperateHandle.ueAddContent.setContent("");
        });
        //模糊搜索
        $('#customSearch').click(function () {
            grid2.fastQueryParameters = new Object();
            grid2.fastQueryParameters['eq_delFlag'] = 1;
            grid2.fastQueryParameters['lk_class_name'] = $('#keyword').val();
            grid2.pager.startRecord = 0;
            grid2.pager.nowPage = 1;
            grid2.pager.recordCount = -1;
            grid2.pager.pageCount = -1;
            grid2.refresh(true);
        });
    }

    /**
     * 删除分类
     */
    function delHelpClass(id) {
        var tpl = '您确定要删除该分类吗?'
        $.ncConfirm({
            url: ncGlobal.adminRoot + "helpclass/json/del",
            data: {
                id: id
            },
            callBack: function (req) {
                if (req.code == 200) {
                    grid2.refresh(true);
                }
            },
            content: tpl
        });
    }

    /**
     * 删除内容
     */
    function delHelpContent(id) {
        var tpl = '您确定要删除该内容吗?'
        $.ncConfirm({
            url: ncGlobal.adminRoot + "helpcontent/json/del",
            data: {
                id: id
            },
            callBack: function (req) {
                if (req.code == 200) {
                    grid1.refresh(true);
                }
            },
            content: tpl
        });
    }

    //外部可调用
    return {
        ueAddContent: _ueAdd,
        ueEditContent: _ueEdit,
        bindEvent: _bindEvent,
        delHelpContent: delHelpContent,
        delHelpClass: delHelpClass
    }
}();

$(function () {
    //加载列表
    grid2.load();
    //页面绑定事件
    OperateHandle.bindEvent();
    OperateHandle.ueAddContent = UE.getEditor('contnetAdd', {textarea: 'content'});
    OperateHandle.ueEditContent = UE.getEditor('contnetEdit', {textarea: 'content'});
});